using System.Windows;
using System.Windows.Controls;
using Tryit.ViewModels;
using System.Windows.Input;
using System.Linq;
using Prism.Ioc;
using System;

namespace Tryit.Views
{
    public partial class InfoSummaryView : Window
    {
        private readonly InfoSummaryViewModel _viewModel;

        public InfoSummaryView()
        {
            InitializeComponent();
            
            // 使用依赖注入获取ViewModel实例
            _viewModel = (Application.Current as App)?.Container.Resolve<InfoSummaryViewModel>();
            DataContext = _viewModel;
        }

        private void FileListDataGrid_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
                if (files.Any(f => System.IO.Path.GetExtension(f).ToLower() == ".xlsx"))
                {
                    e.Effects = DragDropEffects.Copy;
                }
                else
                {
                    e.Effects = DragDropEffects.None;
                }
            }
            e.Handled = true;
        }

        private void FileListDataGrid_DragLeave(object sender, DragEventArgs e)
        {
            e.Handled = true;
        }

        private void ManageTemplateButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var templateManagementView = new TemplateManagementView();
                templateManagementView.Owner = Application.Current.MainWindow;
                templateManagementView.DataContext = DataContext;
                templateManagementView.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"打开模板管理窗口失败：{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (sender is DataGrid dataGrid)
            {
                _viewModel.UpdateSelectedElements(dataGrid.SelectedItems.Cast<SanmaoTools.Models.Element>());
            }
        }
    }
} 